<?php
namespace Model;

use Logger;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;

class ArticleTable extends AbstractTable
{

    /**
     * @param $id
     * @return Article
     */
    public function getArticle($id){
       return $this->getById($id);
    }

    /**
     * @param $header
     * @return Article
     */
    public function getArticleByHeader($header)
    {
       return $this->getByColumn('header', $header)->current();
    }

    /**
     * @return InputFilter
     */
    public function getInputFilter()
    {
        $filter = new InputFilter();

        return $filter;
    }

    public function searchAllArticle($isPaging, $offset, $rowPerPage, $orderArr = array('name'), $searchOption){


    }

    public function searchArticle($isPaging, $offset, $rowPerPage , $orderArray ,$searchOption )
    {
        $result=array();
        $option = array();

        $select = $this->getDbSqlSelect();

        if($searchOption['header']){
            $select->where(array('header like "%'.$searchOption['header'].'%"'));
        }

//        $select->setIntegrityCheck(false);
//        $select->reset(Zend_Db_Select::COLUMNS);

        if($isPaging){
            $select->offset($offset)->limit($rowPerPage);
        }

        if($orderArray){
            $select->order($orderArray);
        }

        $select->columns(array('id', 'header', 'active',
            'order_number', 'user_name', 'created_date', 'created_by'));

        if($searchOption["categoryId"] && $searchOption["categoryId"] !='ALL'){
            $categoryService = new Category();
            $allParent = $categoryService->getAllChildren($searchOption["categoryId"]);
            $tmp = ','.$searchOption["categoryId"];
            foreach ($allParent as $parent) {
                $tmp = $tmp.",".$parent->id;
            }
            $tmp = '('.substr($tmp,1).')';

            $select->join(array('aac' => "article_category"), "article.id = aac.article_id", array());
            $select->where("aac.category_id in ".$tmp)
                ->where('is_primary = ?', 1);
        }

        if($searchOption["subCategoryId"] && $searchOption["subCategoryId"] !='ALL'){
            $categoryService = new Category();
            $allParent = $categoryService->getAllChildren($searchOption["subCategoryId"]);
            $tmp = ','.$searchOption["subCategoryId"];
            foreach ($allParent as $parent) {
                $tmp = $tmp.",".$parent->id;
            }
            $tmp = '('.substr($tmp,1).')';

            $select->join(array('aac2' => "article_category"), "article.id = aac2.article_id", array());
            $select->where("aac2.category_id in ".$tmp)
                ->where('is_primary = ?', 0);
        }

        $select->join(array('ur' => "user"), "article.created_by = ur.id", array("user_name"=>"username"));

//        Logger::debug($select->getSqlString($this->tableGateway->adapter->getPlatform()));
        $allRow = $this->fetchAll($select);

        $total = $this->getCount($select);

        $result["total"] = $total;
        $result["rows"] = $allRow;
        return $result;
    }

    /**
     * @return \Zend\Db\ResultSet\ResultSet
     */
    public function getAllActiveArticle(){
        return $this->getTableGateway()->select(array('active'=>1));
    }






}